package com.itwn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itwn.entity.Schedule;
import com.itwn.mapper.ScheduleMapper;
import com.itwn.mapper.ScheduleSeatMapper;
import com.itwn.mapper.SeatMapper;
import com.itwn.vo.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ScheduleService {
    @Autowired
    ScheduleMapper scheduleMapper;
    @Autowired
    ScheduleSeatMapper scheduleSeatMapper;
    @Autowired
    SeatMapper seatMapper;
    public int addSchedule(Schedule schedule){
        try {
            scheduleMapper.insert(schedule);
            Integer scheduleId = schedule.getScheduleId();
            List<Integer> seats = seatMapper.selectAllSeats(scheduleId);
            scheduleSeatMapper.insertScheduleSeats(scheduleId,seats);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public PageInfo<ScheduleQueryVo> pageSearch(Integer pageNum, Integer pageSize, int cinemaHallId, String movieName, Date startTime, Date endTime) {
        PageHelper.startPage(pageNum,pageSize);
        List<ScheduleQueryVo> list = scheduleMapper.selectByPage(cinemaHallId, movieName, startTime, endTime);
        PageInfo<ScheduleQueryVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    public int remove(Integer scheduleId) {
        return scheduleMapper.updateState(scheduleId);
    }

    public int modify(Schedule schedule) {
        return scheduleMapper.updateByPrimaryKey(schedule);
    }

    public int findRepeatedSchedule(Integer cinemaHallId, Integer movieId, Date startTime, Date endTime) {
        return scheduleMapper.isRepeated(cinemaHallId,movieId,startTime,endTime);
    }

    public int findRepeatedScheduleForUpdate(int cinemaHallId, Date startTime, Date endTime) {
        return scheduleMapper.isRepeatedForUpdate(cinemaHallId,startTime,endTime);
    }
}